package com.markhsiu.core.constant;

/**
 * 大部分用户的操作系统（如windows, FreeBsd,Linux）是Little Endian的。
 *  少部分，如MAC OS ,是Big Endian 的。
 *  
 *  字节转换，当前值支持Little Endian
 * @author Mark Hsiu
 *
 */
public class Bits {  
	

    private Bits() { }

    public static void put(byte[] data, int offset , byte val) {
    	 data[offset  ] = val;
    }
    
    // -- get/put char --

      static private char makeChar(byte b1, byte b0) {
        return (char)((b1 << 8) | (b0 & 0xff));
    }

    public static char toChar(byte b0, byte b1) {
        return makeChar(b0, b1);
    }
    


    private static byte char1(char x) { return (byte)(x >> 8); }
    private static byte char0(char x) { return (byte)(x     ); }

     
    public  static void putChar( byte[] data, int offset , char val) {
    	data[offset  ] = char1(val);
    	data[offset+1] = char0(val);
    }
    


    // -- get/put short --

      static private short makeShort(byte b1, byte b0) {
        return (short)((b1 << 8) | (b0 & 0xff));
    }

    public static short toShort(byte b0, byte b1) {
        return makeShort(b0, b1);
    }
    

    private static byte short1(short x) { return (byte)(x >> 8); }
    private static byte short0(short x) { return (byte)(x     ); }

    public  static void putShort( byte[] data, int offset , short val) {
    	data[offset  ] = short1(val);
    	data[offset+1] = short0(val);
    }


    // -- get/put int --
    
    public static void putInt( byte[] data, int offset , int val) {
    	data[offset  ] = int3(val);
    	data[offset+1] = int2(val);
    	data[offset+2] = int1(val);
    	data[offset+3] = int0(val);
    }

    
     static private int makeInt(byte b3, byte b2, byte b1, byte b0) {
        return (((b3       ) << 24) |
                ((b2 & 0xff) << 16) |
                ((b1 & 0xff) <<  8) |
                ((b0 & 0xff)      ));
    }

    public static int toInt(byte b0, byte b1, byte b2, byte b3) {
    	return makeInt(b0, b1, b2, b3);
    }
    

    private static byte int3(int x) { return (byte)(x >> 24); }
    private static byte int2(int x) { return (byte)(x >> 16); }
    private static byte int1(int x) { return (byte)(x >>  8); }
    private static byte int0(int x) { return (byte)(x      ); }


    // -- get/put long --

      static private long makeLong(byte b7, byte b6, byte b5, byte b4,
                                 byte b3, byte b2, byte b1, byte b0)
    {
        return ((((long)b7       ) << 56) |
                (((long)b6 & 0xff) << 48) |
                (((long)b5 & 0xff) << 40) |
                (((long)b4 & 0xff) << 32) |
                (((long)b3 & 0xff) << 24) |
                (((long)b2 & 0xff) << 16) |
                (((long)b1 & 0xff) <<  8) |
                (((long)b0 & 0xff)      ));
    }

    public static long toLong(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) {
  		return makeLong(b0, b1, b2, b3, b4, b5, b6, b7);
  	}
 


     

   


    // -- get/put float --


    // -- get/put Double --
    
    public static double toDouble(byte b0, byte b1, byte b2, byte b3, byte b4, byte b5, byte b6, byte b7) {
  		return Double.longBitsToDouble(makeLong(b0, b1, b2, b3, b4, b5, b6, b7));
  	}


 
   
}
